const db = uniCloud.database()

import store from '../store/index.js'

async function insert(tableName, data, condition) {
	if (condition) {
		let [unique, msg] = await checkUnique(tableName, condition)
		if (!unique) {
			return [false, '已存在']
		}
	}
	const table = db.collection(tableName)
	let res = await table.add(data)
	if (res.result.errCode == 0) {
		return [true, res.result.id, '']
	}
	return [false, [], '添加失败']
}

//如果存在，返回假；如果不存在，返回真；
async function checkUnique(tableName, fields) {
	const table = db.collection(tableName)
	for (let field of fields) {
		let res = await table.where(field).get()
		if (res.result.data.length > 0) {
			return [false, res.result.data]
		}
	}
	return [true, '']
}




async function del(tableName, condition) {
	const table = db.collection(tableName)
	let res = await table.where(condition).remove()
	if (res.result.affectedDocs > 0) {
		return true
	} else {
		return false
	}
}
async function getData(tableName, condition) {
	if (condition) {
		let res = await db.collection(tableName).where(condition).get()
		return res.result.data
	} else {
		let res = await db.collection(tableName).get()
		return res.result.data
	}
}
async function update(tableName, condition, updateData) {
	let [unique, msg] = await checkUnique(tableName, [condition])
	if (!unique) {
		const table = db.collection(tableName)
		let res = await table.where(condition).update(updateData)
		if (res) {
			return [res, '更新成功']
		}
	} else {
		let [res, data, msg] = await insert(tableName, condition, '')
		if (res) {
			return [res, '更新成功']
		}
	}
	return [false, '更新失败']
}
async function getCourse() {
	let datas = await getData('sportCourse')
	let courses = []
	if (datas.length > 0) {
		for (let course of datas) {
			courses.push({
				id: course._id,
				src: course.course_src,
				name: course.course_name,
				desc: course.course_desc
			})
		}
	}
	return courses
}

async function getUserInfo(condition) {
	let datas = await getData('admin', condition)
	return datas[0]
}
async function getKgG() {
	let datas = await getData('sportKG')
	let kgs = []
	if (datas.length > 0) {
		for (let kg of datas) {
			kgs.push({
				id: kg._id,
				src: kg.kg_src,
				name: kg.kg_name,
				desc: kg.kg_detail
			})
		}
	}
	return kgs
}

function tranformDate(time) {
	return time.split('T')[0].replace(/\//g, '-')
}
async function getUser() {
	let datas = await getData('user')
	let users = []
	if (datas.length > 0) {
		for (let user of datas) {
			users.push({
				id: user._id,
				account: user.user_account,
				name: user.user_name,
				email: user.user_email,
				avatar: user.user_avatar,
				height: user.height,
				pwd: user.user_pwd,
				weigh: user.weigh,
				birthdate: tranformDate(user.birthdate),
				status: user.user_status
			})
		}
	}
	return users
}
async function getSport() {
	let datas = await getData('sport')
	let sports = []
	if (datas.length > 0) {
		for (let sport of datas) {
			sports.push({
				id: sport._id,
				src: sport.sport_src,
				name: sport.sport_name,
				calorie: sport.sport_calorie,
				stype: sport.sport_type
			})
		}
	}
	return sports
}
async function getAdmin() {
	let datas = []
	let account = store.getters.getUserAccount
	let issuper = store.getters.getIsSuper
	if (issuper) {
		datas = await getData('admin', `(admin_role==1 || admin_role==2) && admin_account!="${account}"`)
	} else {
		datas = await getData('admin', `admin_role==2  && admin_account!="${account}"`)
	}
	let admins = []
	if (datas.length > 0) {
		for (let admin of datas) {
			admins.push({
				id: admin._id,
				account: admin.admin_account,
				name: admin.admin_name,
				avatar: admin.admin_avatar,
				pwd: admin.admin_pwd,
				role: admin.admin_role
			})
		}
	}
	return admins
}
export const addData = insert
export const check = checkUnique
export const verify = update
export const remove = del
export const getCourseData = getCourse
export const getKGData = getKgG
export const getUserData = getUser
export const getAdminData = getAdmin
export const getSportData = getSport
export const getUserInfoData = getUserInfo